package com.lsyw.intergrate.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lsyw.intergrate.entity.*;
import com.lsyw.intergrate.service.MassifPlanService;
import com.lsyw.intergrate.service.MassifService;
import org.springframework.util.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.lsyw.intergrate.entity.common.PageInfo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lsyw.intergrate.common.Result;
import org.springframework.util.CollectionUtils;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import com.lsyw.intergrate.service.MassifPlanAlarmService;
import com.lsyw.intergrate.entity.MassifPlanAlarm;

import java.util.*;

import io.swagger.annotations.Api;
import lombok.Data;

import org.springframework.stereotype.Controller;

/**
 * <p>
 * 工地修复进度计划告警 前端控制器
 * </p>
 *
 * @author 刘磊
 * @since 2024-02-21
 */
@Api(tags = "工地修复进度计划告警")
@RestController
@RequestMapping("/massif-plan-alarm")
public class MassifPlanAlarmController {


    @Autowired
    private MassifPlanAlarmService massifPlanAlarmService;
    @Autowired
    private MassifPlanService massifPlanService;
    @Autowired
    private MassifService massifService;

    /**
     * 查询列表
     */
    @PostMapping("/list")
    public Result list(@RequestBody MassifPlanAlarmReqVo massifPlanAlarm) {
        final LambdaQueryWrapper<MassifPlanAlarm> lambda = new QueryWrapper<MassifPlanAlarm>().lambda();
        this.buildCondition(lambda, massifPlanAlarm);
        return Result.successResult(massifPlanAlarmService.list(lambda));
    }

    /**
     * 分页查询
     */
    @PostMapping("/page")
    public Result page(@RequestBody PageInfo pageInfo) {
        Page<MassifPlanAlarm> page = new Page<>();
        page.setSize(pageInfo.getPageSize());
        page.setCurrent(pageInfo.getPageNum());
        final LambdaQueryWrapper<MassifPlanAlarm> lambda = new QueryWrapper<MassifPlanAlarm>().lambda();
        IPage<MassifPlanAlarm> iPage = this.massifPlanAlarmService.page(page, lambda);
        iPage.getRecords().forEach(e -> {
            MassifPlan massifPlan=massifPlanService.getById(e.getMassifPlanId());
            Massif massif=massifService.getById(massifPlan.getMassifId());
            e.setMassifName(massif.getName());
        });
        return Result.successResult(iPage);
    }
    /**
     * 通过id查询
     */
    @GetMapping("/updateStatus")
    public Result updateStatus(String id,Integer status) {
        MassifPlanAlarm massifPlanAlarm = massifPlanAlarmService.getById(id);
        massifPlanAlarm.setStatus(status);
        massifPlanAlarmService.updateById(massifPlanAlarm);
        return Result.successResult("");
    }



    /**
     * 构造查询条件
     *
     * @param lambda
     * @param param
     */
    private void buildCondition(LambdaQueryWrapper<MassifPlanAlarm> lambda, MassifPlanAlarmReqVo param) {
        if (!StringUtils.isEmpty(param.getId())) {
            lambda.eq(MassifPlanAlarm::getId, param.getId());
        }
        if (!StringUtils.isEmpty(param.getMassifPlanId())) {
            lambda.eq(MassifPlanAlarm::getMassifPlanId, param.getMassifPlanId());
        }
        if (!StringUtils.isEmpty(param.getContent())) {
            lambda.eq(MassifPlanAlarm::getContent, param.getContent());
        }
        if (!CollectionUtils.isEmpty(param.getDateList())) {
            lambda.ge(MassifPlanAlarm::getDate, param.getDateList().get(0));
            lambda.le(MassifPlanAlarm::getDate, param.getDateList().get(1));
        }
        if (!StringUtils.isEmpty(param.getStatus())) {
            lambda.eq(MassifPlanAlarm::getStatus, param.getStatus());
        }
        if (!StringUtils.isEmpty(param.getGmtCreateUser())) {
            lambda.eq(MassifPlanAlarm::getGmtCreateUser, param.getGmtCreateUser());
        }
        if (!CollectionUtils.isEmpty(param.getGmtCreateList())) {
            lambda.ge(MassifPlanAlarm::getGmtCreate, param.getGmtCreateList().get(0));
            lambda.le(MassifPlanAlarm::getGmtCreate, param.getGmtCreateList().get(1));
        }
        if (!StringUtils.isEmpty(param.getGmtModifiedUser())) {
            lambda.eq(MassifPlanAlarm::getGmtModifiedUser, param.getGmtModifiedUser());
        }
        if (!CollectionUtils.isEmpty(param.getGmtModifiedList())) {
            lambda.ge(MassifPlanAlarm::getGmtModified, param.getGmtModifiedList().get(0));
            lambda.le(MassifPlanAlarm::getGmtModified, param.getGmtModifiedList().get(1));
        }
        if (!StringUtils.isEmpty(param.getDelFlag())) {
            lambda.eq(MassifPlanAlarm::getDelFlag, param.getDelFlag());
        }
        lambda.orderBy(true, false, MassifPlanAlarm::getId);
    }


    /**
     * 请求model
     */
    @Data
    @AllArgsConstructor
    private static class MassifPlanAlarmReqVo extends MassifPlanAlarm {
        private List<String> dateList; // 日期起止
        private List<String> gmtCreateList; // 创建时间起止
        private List<String> gmtModifiedList; // 更新时间起止
    }


}
